package de.karlNet.sambamanager.view.beans.forms;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Controller;

import de.karlNet.sambamanager.ldapController.AbstractLdapTree;
import de.karlNet.sambamanager.ldapController.OriginalLdapTree;
import de.karlNet.sambamanager.ldapController.UserGroupOnlyTree;
import de.karlNet.sambamanager.ldapModel.ITypeCheckerCallback;
import de.karlNet.sambamanager.ldapModel.LdapContainer;
import de.karlNet.sambamanager.ldapModel.LdapGroup;
import de.karlNet.sambamanager.ldapModel.LdapNestable;
import de.karlNet.sambamanager.ldapModel.LdapObject;
import de.karlNet.sambamanager.ldapModel.LdapUser;
import de.karlNet.sambamanager.view.beans.ManualLeafHandlingTreeNode;

@Controller
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class SelectableUserGroupTree extends SelectableTree {
	@Autowired
	private UserGroupOnlyTree groupOnlyTree;

	@Autowired
	private OriginalLdapTree originalLdapTree;
	
	@Override
	protected AbstractLdapTree getLdapTree() {
		return this.groupOnlyTree;
	}

	public List<LdapNestable> getSelectedUserGroups() {
		List<LdapObject> ldapObjects = this.getSelectedLdapNestable();
		final List<LdapNestable> ldapNestables = new ArrayList<LdapNestable>();
		for (LdapObject ldapObject : ldapObjects) {
			ldapObject.isType(new ITypeCheckerCallback() {
				@Override
				public void isGroup(LdapGroup ldapGroup) {
					ldapNestables.add(ldapGroup);
				}
				
				@Override
				public void isUser(LdapUser ldapUser) {
					ldapNestables.add(ldapUser);
				}
				
				@Override
				public void isContainer(LdapContainer ldapContainer) {
					List<LdapObject> objectsOfContainer = originalLdapTree.getAllChildrenOfDN(ldapContainer.getDn());
					for (LdapObject subObject : objectsOfContainer) {
						subObject.isType(new ITypeCheckerCallback() {
							@Override
							public void isGroup(LdapGroup ldapGroup) {
								ldapNestables.add(ldapGroup);
							}
							
							@Override
							public void isUser(LdapUser ldapUser) {
								ldapNestables.add(ldapUser);
							}
						});
					}
				}
			});
		}
		return ldapNestables;
	}
	

	@Override
	protected ITypeCheckerCallback allowedTypes(
			final ManualLeafHandlingTreeNode handlingTreeNode) {
		return new ITypeCheckerCallback() {
			public void isGroup() {
				handlingTreeNode.setSelectable(true);
				handlingTreeNode.setType("group");
			}

			public void isUser() {
				handlingTreeNode.setSelectable(true);
				handlingTreeNode.setType("user");
			}

			public void isNestable() {
				handlingTreeNode.setSelectable(false);
			}

			public void isLdapObject() {
				handlingTreeNode.setSelectable(false);
				handlingTreeNode.setType("ldapObject");
			}

			public void isContainer() {
				handlingTreeNode.setSelectable(true);
				handlingTreeNode.setType("container");
			}
		};
	}
}
